import { fileURLToPath, URL } from "url";
import { generateModifyVars } from "./config/vite/generate/generateModifyVars";
import { ConfigEnv, searchForWorkspaceRoot, UserConfig } from "vite";
import path from "path";
import {
  themePreprocessorPlugin,
  themePreprocessorHmrPlugin,
} from "@zougt/vite-plugin-theme-preprocessor";
import { createVitePlugins } from "./config/vite/plugin/index";
import { configManualChunk } from "./config/vite/optimizer";
// https://vitejs.dev/config/

export default ({ command, mode }: ConfigEnv): UserConfig => {
  const isBuild = mode !== "dev";
  return {
    base: "./",
    plugins: [
      ...createVitePlugins(isBuild),
      themePreprocessorPlugin({
        less: {
          // 启用任意主题色模式
          arbitraryMode: false,
          // 默认的主题色，用于对其他颜色值形成对比值，通常与 src/theme/theme-vars.less 中的一个主题色相同，也可以不相同，就看是不是你想要的效果
          defaultPrimaryColor: "#512da7",
          // 只需提供一组变量文件，变量文件内容不应该夹带样式代码，设定上只需存在变量
          multipleScopeVars: [
            {
              scopeName: "theme-default",
              path: path.resolve("src/assets/theme/default.less"),
            },
            {
              scopeName: "theme-green",
              path: path.resolve("src/assets/theme/green.less"),
            },
          ],
          // css中不是由主题色变量生成的颜色，也让它抽取到主题css内，可以提高权重
          includeStyleWithColors: [
            {
              // color也可以是array，如 ["#ffffff","#000"]
              // color: "#ffffff",
              // 排除属性，如 不提取背景色的#ffffff
              // excludeCssProps:["background","background-color"]
              // 排除选择器，如 不提取以下选择器的 #ffffff
              // excludeSelectors: [
              //   ".ant-btn-link:hover, .ant-btn-link:focus, .ant-btn-link:active",
              // ],
            },
          ],
        },
      }),
      // themePreprocessorHmrPlugin(),
    ],
    define: {
      __VUE_PROD_DEVTOOLS__: true,
    },
    server: {
      host: "0.0.0.0",
      fs: {
        allow: [
          // 搜索工作区的根目录
          searchForWorkspaceRoot(process.cwd()),
          "/Users/xuyifei/Documents/study/npm/ant_vue3_ui_ts/lib/",
        ],
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          // modifyVars: generateModifyVars(),
          javascriptEnabled: true,
          // multipleScopeVars: [],
        },
        // scss:{
        //   additionalData:'@import "./src/assets/main.scss";'
        // }
      },
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    build: {
      target: "es2015",
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: true,
        },
      },
      // minify: "terser",
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
          manualChunks: configManualChunk,
          // inlineDynamicImports: true, //支持动态导入
        },
      },
    },
  };
};
